﻿using Newtonsoft.Json.Linq;
using OnlineStore.Tools;
using System.IO;
using System.Linq;

namespace OnlineStore
{
    public class FilePath
    {
        public FilePath(bool test)
        {
            if (test)
            {
                rootPath = Directory.GetCurrentDirectory().Replace("\\bin\\Debug", "");
            }
            else
            {
                rootPath = Directory.GetCurrentDirectory();
            }

            config = rootPath + @"\Config\";
            navigation = rootPath + @"\Navigation\";
            defaultConfig = rootPath + @"\Default\Config\";
            defaultNavigation = rootPath + @"\Default\Navigation\";
            carPic = config + "carPic.txt";
            userConfig = config + "userConfig.json";
            pwSttingConfig = config + "pwSettingConfig.json";
            tasksConfig = config + "tasksConfig.json";
            settingConfig = config + "settingConfig.json";
            taobaoNavigation = navigation + "taobaoNavigation.json";
            tianmaoNavigation = navigation + "tianmaoNavigation.json";
            pinduoduoNavigation = navigation + "pinduoduoNavigation.json";
            first = rootPath + @"\Default\" + "first.txt";
        }

        private string rootPath;
        private string config;
        private string navigation;
        //Default
        private string defaultConfig;
        private string defaultNavigation;
        //Config
        public string carPic;
        public string userConfig;
        public string pwSttingConfig;
        public string tasksConfig;
        public string settingConfig;
        //Navigation
        public string taobaoNavigation;
        public string tianmaoNavigation;
        public string pinduoduoNavigation;
        //first
        public string first;

        /// <summary>
        /// 判断是否为安装后首次运行，如果是，则检查配置文件是否存在异常，执行检查完毕后，删除首次运行校验文件
        /// </summary>
        public void CheckFirst()
        {
            if (!File.Exists(first)) return;
            string isFirst = File.ReadAllText(first).Trim();
            if (isFirst == "0") return;

            CheckFiles(defaultConfig, config);
            CheckFiles(defaultNavigation, navigation);

            if (UpdataFiles.congfigs.Length > 0) 
            {
                foreach (var objson in UpdataFiles.congfigs)
                {
                    string oldfile = $"{config}{objson.ToString().Trim()}";
                    string newfile = $"{defaultConfig}{objson.ToString().Trim()}";
                    File.Copy(newfile, oldfile, true);
                }
            }

            if (UpdataFiles.navigationa.Length > 0)
            {
                foreach (var objson in UpdataFiles.navigationa)
                {
                    string oldfile = $"{navigation}{objson.ToString().Trim()}";
                    string newfile = $"{defaultNavigation}{objson.ToString().Trim()}";
                    File.Copy(newfile, oldfile, true);
                }
            }

            File.WriteAllText(first, "0");
        }

        /// <summary>
        /// 检查目标文件夹与源文件夹是否存在差异，如果有差异，则将源文件夹中的文件复制到目标文件夹中
        /// </summary>
        /// <param name="sourceDir">源文件夹</param>
        /// <param name="targetDir">目标文件夹</param>
        public void CheckFiles(string sourceDir,string targetDir)
        {
            if (!Directory.Exists(targetDir)) 
            {
                Directory.CreateDirectory(targetDir);
                string[] sourceFiles = Directory.GetFiles(sourceDir);
                foreach (string sourceFile in sourceFiles) 
                {
                    string targetFile = targetDir + sourceFile.Replace(sourceDir, "");
                    File.Copy(sourceFile, targetFile, true);
                }
            }
            else
            {
                string[] targetFiles = Directory.GetFiles(targetDir);
                string[] sourceFiles = Directory.GetFiles(sourceDir);
                if (targetFiles.Length != sourceFiles.Length)
                {
                    foreach (string sourceFile in sourceFiles)
                    {
                        string targetFile = targetDir + sourceFile.Replace(sourceDir, "");
                        if (!File.Exists(targetFile)) 
                        {
                            File.Copy(sourceFile, targetFile);
                        }
                    }
                }
            }
        }

        public void CleanDir(string path)
        {
            Directory.Delete(path, true );
        }
    }
}
